package com.woniu.darryringmall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.darryringmall.admin.Request.SpecVO;
import com.woniu.darryringmall.front.entity.Spec;
import com.woniu.darryringmall.front.entity.SpecValue;
import com.woniu.darryringmall.front.service.SpecService;
import com.woniu.darryringmall.front.service.SpecValueService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import com.woniu.darryringmall.utils.ResultStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("adminSpec")
@RequestMapping("admin/spec")
public class SpecController {
    @Autowired
    private SpecService specService;
    @Autowired
    private SpecValueService specValueService;

    @RequestMapping("add")
    public ResponseData add(@RequestBody Spec spec) {
        QueryWrapper<Spec> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", spec.getName());
        queryWrapper.eq("categoryid", spec.getCategoryId());
        if (spec.getName() == null || spec.getName().trim().length() < 2) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格名必须在2位以上");
        }
        if (specService.getOne(queryWrapper) != null) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格名不能重复");
        }
        specService.save(spec);
        return ResponseUtils.SUCCESS;
    }

    @RequestMapping("update")
    public ResponseData update(@RequestBody Spec spec) {
        UpdateWrapper<Spec> updateWrapper = new UpdateWrapper<>();
        Spec oldSpec = specService.getById(spec.getId());
        if (!oldSpec.getName().equals(spec.getName())) {
            if (spec.getName() == null || spec.getName().trim().length() < 2) {
                return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格名必须在2位以上");
            }
            QueryWrapper<Spec> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", spec.getName());
            if (specService.getOne(queryWrapper) != null) {
                return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格名不能重复");
            }
            updateWrapper.set("name", spec.getName());
            updateWrapper.eq("id", spec.getId());
            specService.update(updateWrapper);
        }
        return ResponseUtils.SUCCESS;
    }

    @RequestMapping("list/{id}")
    public ResponseData list(@PathVariable int id) {
        QueryWrapper<Spec> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryid", id);
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", specService.list(queryWrapper));
    }

    @RequestMapping("getSpecAndSpecValue/{id}")
    public ResponseData getAll(@PathVariable int id) {
        QueryWrapper<Spec> specQueryWrapper = new QueryWrapper<>();
        specQueryWrapper.eq("categoryid", id);
        List<Spec> specs = specService.list(specQueryWrapper);
        List<SpecVO> specVOS = new ArrayList<>();
        specs.forEach(e -> {
            QueryWrapper<SpecValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("specid", e.getId());
            List<SpecValue> specValues = specValueService.list(queryWrapper);
            SpecVO specVO = new SpecVO();
            specVO.setSpec(e);
            specVO.setSpecValueList(specValues);
            specVOS.add(specVO);
        });
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", specVOS);
    }

    @RequestMapping("del/{id}")
    public ResponseData del(@PathVariable int id) {
        specService.removeById(id);
        return ResponseUtils.SUCCESS;
    }
}
